package com.glory.dgcpcpda.activity.mfg;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.ChangeShifts2Adapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.ChangeShiftEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ShiftExchange2Activity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etShiftNo)
    FreeUI_ClearEditText etShiftNo;
    @BindView(R.id.etWorkNo)
    FreeUI_ClearEditText etWorkNo;
    @BindView(R.id.tvShiftTime)
    TextView tvShiftTime;
    @BindView(R.id.etShift2No)
    FreeUI_ClearEditText etShift2No;
    @BindView(R.id.etComment)
    FreeUI_ClearEditText etComment;
    @BindView(R.id.tvGetData)
    TextView tvGetData;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.list_tv5)
    TextView listTv5;
    @BindView(R.id.list_tv6)
    TextView listTv6;
    @BindView(R.id.list_tv7)
    TextView listTv7;
    @BindView(R.id.rvWorkNoList)
    RecyclerView rvWorkNoList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private ChangeShifts2Adapter mChangeShiftsAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_shift_exchange2;
    }

    @Override
    protected void initView() {
        initLayout();
        mChangeShiftsAdapter = new ChangeShifts2Adapter(null);
        mChangeShiftsAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mChangeShiftsAdapter.isFirstOnly(true);
        rvWorkNoList.setAdapter(mChangeShiftsAdapter);

    }

    private void initLayout() {
        tvTitle.setText("接班");
        setMainLayout(llMain);
        listTv1.setText("单据号");
        listTv2.setText("交班人工号");
        listTv3.setText("批号");
        listTv4.setText("交班数量");
        listTv5.setText("设备ID");
        listTv6.setText("批次状态");
        listTv7.setText("接班数量");
        listTv5.setVisibility(View.VISIBLE);
        listTv6.setVisibility(View.VISIBLE);
        listTv7.setVisibility(View.VISIBLE);
        initRecyclerView(rvWorkNoList,this);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit,R.id.tvGetData})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvBaseSubmit:
                if (actionControl("Submit18")){
                    return;
                }

                if (getText(etShift2No).equals("")){
                    showToast("接班人工号不能为空",false);
                    return;
                }
                showSubmitDialog(() -> submit("TO",false));
                break;
            case R.id.tvGetData:
                if (getText(etShiftNo).equals("") && getText(etWorkNo).equals("")){
                    showToast("交班人工号或单据号，必填一项",false);
                    return;
                }
                mChangeShiftsAdapter.getData().clear();
                mChangeShiftsAdapter.setShiftNo("");
                submit("SELECT",true);
                break;
        }

    }

    private void submit(String actionType,boolean action){
        FreeUI_EntityCallBack<BaseResponseBean<ChangeShiftEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<ChangeShiftEntity>>
                (new TypeToken<BaseResponseBean<ChangeShiftEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                    if (!action){
                        showConfirmDialog(ShiftExchange2Activity.this,"接班成功");
                        return;
                    }
                    List<ChangeShiftEntity.ChangeShift> mDataList = response.body().getResponse().getBody().getCHANGESHIFTLINELIST();
                    if (mDataList.size() <= 0){
                        showToast("未查询到交接班信息",false);
                        return;
                    }
                    mChangeShiftsAdapter.setShiftNo(response.body().getResponse().getBody().getREFEROWNER());
                    mChangeShiftsAdapter.setNewData(mDataList);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"CHANGESHIFT");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE",actionType);
        map.addProperty("REFEROWNER",getText(etShiftNo));
        map.addProperty("TOREFER",getText(etShift2No));
        map.addProperty("DOCID",getText(etWorkNo));
        NetUtils.requestPostNet("CPC.CHANGESHIFT","CHANGESHIFT",this,map,callBack);
    }

}
